This research aims to determine if the postoperative function of a total knee replacement recipient can be modeled based on anthropomorphic data, and details of the surgical procedure. We decided to carry out testing on models with two postoperative scoring systems as the response variable. The first score is the “Surgeon’s Score” which measures more objective postoperative metrics about the condition of the replaced knee following surgery. These metrics include: how straight or how flexed the patient can make their knee, how well aligned the knee appears when inspecting from a frontal perspective, and how much laxity or separation can be achieved by placing the lower leg under tension. Finally, the patient is asked about the overall pain they experience with the knee. The resulting score is a value between 0-100 with 80-100 being an excellent result.
The second score we modeled is the “Patient’s Score” which is generated by three questions: how far are they able to walk, how well can they navigate stairs, and do they require any walking aids (crutches, cane, etc).
We hope to create a model that can predict each of the scores based on a variety of categorical and numeric predictors.
This data has been collected from a Southern California Hospital in collaboration with the Shiley Center for Orthopedic Research. The data is used for both clinical research and postoperative monitoring of implant survivorship. All patient specific information has been removed from the data so it adheres to the Health Insurance Portability and Accountability Act (HIPAA).
We formed our group by advertising on the course forum site and Slack chat. After the group had been formed, the first step was to decide on a topic and a data set for the selected topic. Next, after our proposal had been approved, we cleaned up the data and started working towards finding a final model that is most suitable for our data set. To reach the final model, each of us, worked independently to come up with a list of models. Models were presented and discussed during our regular meetings via Zoom where ideas were exchanged. Eventaully, we converged and decided on a final model that has been used for the work presented here.
We hope to determine the impact a particular surgeon or the type of implants used has on the patient’s outcome after surgery. We are also interested in exploring the potential relationship between anthropomorphic factors and the patient’s postoperative satisfaction. Finally, we would like to understand if the considerable expense of computer navigation in knee replacement leads to better postoperative scores. Nevertheless, we keep our options opened to investigate other aspects that were not ‘visible’ or thought about but might reveal themselves as we move on with the project.
Here we may consider KneeScore_Surgeon or KneeScore_Patient as response and Age, Gender, Weight, Height, BMI, Race, Side, Manufacturer as predictors. Gender, Race, Side and Manufacturer are some of the categorical predictors and Age, Height, Weight and BMI are numerical predictors.
knee = read.csv('knees.csv')
knee$Surgeon = as.factor(knee$Surgeon)
knee$Year = as.factor(knee$Year)
#Function to plot various plots as QQPlot, Fitted vs Residual Plot.
showPlots = function(model) {
par(mfcol=c(1,3))
qqnorm(resid(model), col = red, pch = 16)
qqline(resid(model), col = lightblue, lwd = 2)
plot(fitted(model), resid(model), xlab = 'Fitted Values', ylab = 'Residuals', main = 'Fitted vs. Residuals Plot', col = purple, pch = 16)
abline(h = 0, col = green, lwd = 2)
hist(resid(model), col = blue, main = 'Histogram of Residuals', xlab = 'Residuals')
}
#Function to calculate various statistics which we use to evaluate the model
evaluate = function(model){
# Run each of the tests
lambda = 1.000001 # this is a hack so the function will return a CrossValidation score - HatValues of 1.0 are causing a division by zero error.
shapiroTest = shapiro.test(resid(model))
bpTest = bptest(model)
rSquared = summary(model)$r.squared
adjRSquared = summary(model)$adj.r.squared
loocv = sqrt(mean((resid(model) / (lambda - hatvalues(model))) ^ 2))
large_hat = sum(hatvalues(model) > 2 * mean(hatvalues(model)))
large_resid = sum(rstandard(model)[abs(rstandard(model)) > 2]) / (length(rstandard(model) + lambda))
# Collect tests in dataframe
values = data.frame(Result = c(prettyNum(shapiroTest$p.value), prettyNum(bpTest$p.value), prettyNum(rSquared), prettyNum(adjRSquared),prettyNum(loocv), prettyNum(large_hat)))
rowNames = data.frame(Test = c('Shapiro Wilk pvalue', 'Breusch-Pagan pvalue', 'R Squared', 'Adj R Squared', 'Cross Validation', 'Large Hat Values'))
summary_output = cbind(rowNames, values)
show(summary_output)
}
# find and remove all influential values and return the resulting dataframe
removeCooks = function(model, data){
cooks = which(cooks.distance(model) > 4 / length(cooks.distance(model)))
newData = data[-cooks, ]
return(newData)
}
# identify all influential data points
IdentifyInfluential = function(model, data){
large_lev_index = which(hatvalues(model) > 2 * mean(hatvalues(model)))
cooks = which(cooks.distance(model) > 4 / length(cooks.distance(model)))
rStandard = abs(rstandard(model))
rStandardIndex = which(rStandard > 2)
index_ = union(union(large_lev_index, cooks), rStandardIndex)
return(index_)
}
# find large hat values, subtract them from the dataframe and return the dataframe
removeLargeHatValues = function(model, data){
largeHat = which(hatvalues(model) > 2 * mean(hatvalues(model)))
newData = data[-largeHat, ]
return(newData)
}
# calculate RMSE
rmse = function(actual, predicted) { sqrt(mean((actual - predicted) ^ 2))}
Before moving onto the formal analysis of our data set, we started by performing visual inspection by plotting dependencies among our the variables. The main focus was to look for any visual relationship between KneeScore_Surgeon, KneeScore_Patient and the rest of the variables. Our choice was to use scatterplotMatrix from the car package as it plots regression lines along with the actual data.
scatterplotMatrix(~ KneeScore_Surgeon + KneeScore_Patient + Surgeon + Year + Age + Gender + Weight + Height, span=0.7, data=knee)
scatterplotMatrix(~ KneeScore_Surgeon + KneeScore_Patient + BMI + Diagnosis + Race + Side + GPS + Manufacturer, span=0.7, data=knee)
scatterplotMatrix(~ KneeScore_Surgeon + KneeScore_Patient + FemoralComponentModel + FemoralComponentSize + FemoralComponentType + TibialTrayModel + TibialInsertType, span=0.7, data=knee)
scatterplotMatrix(~ KneeScore_Surgeon + KneeScore_Patient + TibialTraySize + TibialInsertModel + TibialInsertWidth + PatellaModel + PatellaDiameter, span=0.7, data=knee)
Comments: Based on the above graphics, KneeScore_Surgeon, KneeScore_Patient seems to behave similarly across all variables. This is expected as both, surgeon and patient, should be in consensus in regards to the success or quality of a procedure.
For a better view of the above statement, we plotted the following plot, showing a strong relationship between KneeScore_Surgeon and KneeScore_Patient.
scatterplotMatrix(~ KneeScore_Surgeon + KneeScore_Patient, span=0.7, data=knee)
The distribution of KneeScore_Surgeon which reflects the score of a procedure assigned be the Surgeon is unimodal distribution versus a multimodal distribution of KneeScore_Patient, which is the score assigned by Patient. Our explanation of this is that Surgeon assigns any number between 1 and 100 without any discretion whereas Patient has a tendency to round up the score to the nearest fifth (Ex: 5, 10, 35, 50, etc) which creates the multi-picks of the distribution.
Begin by fitting a full additive model as a baseline and evaluating.
surgeon_full_additive_model = lm(KneeScore_Surgeon ~ ., data = knee)
evaluate(surgeon_full_additive_model)
## Test Result
## 1 Shapiro Wilk pvalue 2.423467e-08
## 2 Breusch-Pagan pvalue 0.6506157
## 3 R Squared 0.2668814
## 4 Adj R Squared 0.2124577
## 5 Cross Validation 14.36707
## 6 Large Hat Values 177
showPlots(surgeon_full_additive_model)
The baseline model has many large hat values. Let’s try to understand why.
largeHat = which(hatvalues(surgeon_full_additive_model) > (2 * mean(hatvalues(surgeon_full_additive_model))))
print(knee[689, ])
## Surgeon Year Age Gender Weight Height BMI Diagnosis
## 689 4 2012 68 Female 162 63 28.72 Osteoarthritis
## Race Side KneeScore_Surgeon KneeScore_Patient GPS
## 689 Mid-East Arabian Right 50 50 No
## Manufacturer FemoralComponentModel FemoralComponentSize
## 689 Stryker Triathlon 4
## FemoralComponentType TibialTrayModel TibialTraySize TibialInsertModel
## 689 cruciate retaining Triathlon 4 Triathlon
## TibialInsertWidth TibialInsertType PatellaModel
## 689 11 high flex Triathlon Asymmetric Patella
## PatellaDiameter
## 689 39
After inspection of the rows that represent the large hat values it seems that there are a preponderance of values that are either errant entries or valid, but rare entries. For example, the Race predictor has only two entries for Mid-East Arabian. First we circle back on the original dataset and clean the identified errant entries.
knee = read.csv('knees.csv')
knee$Surgeon = as.factor(knee$Surgeon)
knee$Year = as.factor(knee$Year)
surgeon_full_additive_model = lm(KneeScore_Surgeon ~ ., data = knee)
evaluate(surgeon_full_additive_model)
## Test Result
## 1 Shapiro Wilk pvalue 2.423467e-08
## 2 Breusch-Pagan pvalue 0.6506157
## 3 R Squared 0.2668814
## 4 Adj R Squared 0.2124577
## 5 Cross Validation 14.36707
## 6 Large Hat Values 177
Refitting the model on the cleaned data only shows a marginal impact on the model so we move on to some other methods.
Next we will try to determine which predictors are significant by comparing the full model to models each with a single predictor removed. Evaluation of this code is set to false for convienience. Comments show the significance of each test.
surgeon_additive_model = lm(KneeScore_Surgeon ~ .-Surgeon, data = knee)
Gender_additive_model = lm(KneeScore_Surgeon ~ .-Gender, data = knee)
Race_additive_model = lm(KneeScore_Surgeon ~ .- Race, data = knee)
Year_additive_model = lm(KneeScore_Surgeon ~ .- Year, data = knee)
Age_additive_model = lm(KneeScore_Surgeon ~ .- Age, data = knee)
Weight_additive_model = lm(KneeScore_Surgeon ~ .- Weight, data = knee)
Height_additive_model = lm(KneeScore_Surgeon ~ .- Height, data = knee)
BMI_additive_model = lm(KneeScore_Surgeon ~ .- BMI, data = knee)
Diagnosis_additive_model = lm(KneeScore_Surgeon ~ .- Diagnosis, data = knee)
Side_additive_model = lm(KneeScore_Surgeon ~ .- Side, data = knee)
KneeScore_Patient_additive_model = lm(KneeScore_Surgeon ~ .- KneeScore_Patient, data = knee)
GPS_additive_model = lm(KneeScore_Surgeon ~ .- GPS, data = knee)
Manufacturer_additive_model = lm(KneeScore_Surgeon ~ .- Manufacturer, data = knee)
FemoralComponentModel_additive_model = lm(KneeScore_Surgeon ~ .- FemoralComponentModel, data = knee)
FemoralComponentSize_additive_model = lm(KneeScore_Surgeon ~ .- FemoralComponentSize, data = knee)
FemoralComponentType_additive_model = lm(KneeScore_Surgeon ~ .- FemoralComponentType, data = knee)
TibialTrayModel_additive_model = lm(KneeScore_Surgeon ~ .- TibialTrayModel, data = knee)
TibialTraySize_additive_model = lm(KneeScore_Surgeon ~ .- TibialTraySize, data = knee)
TibialInsertModel_additive_model = lm(KneeScore_Surgeon ~ .- TibialInsertModel, data = knee)
TibialInsertWidth_additive_model = lm(KneeScore_Surgeon ~ .- TibialInsertWidth, data = knee)
TibialInsertType_additive_model = lm(KneeScore_Surgeon ~ .- TibialInsertType, data = knee)
PatellaModel_additive_model = lm(KneeScore_Surgeon ~ .- PatellaModel, data = knee)
PatellaDiameter_additive_model = lm(KneeScore_Surgeon ~ .- PatellaDiameter, data = knee)
anova(surgeon_additive_model,surgeon_full_additive_model) ## Not significant Pr(<F) 0.2061
anova(Gender_additive_model,surgeon_full_additive_model) ## Not significant Pr(<F) 0.3678
anova(Race_additive_model,surgeon_full_additive_model) ## Not significant Pr(<F) 0.3949
anova(Year_additive_model,surgeon_full_additive_model) ## * Some significance Pr(<F) 0.039
anova(Age_additive_model,surgeon_full_additive_model) ## *** Significant Pr(<F) 1.191e-11
anova(Weight_additive_model,surgeon_full_additive_model) ## Not significant Pr(<F) 0.4295
anova(Height_additive_model,surgeon_full_additive_model) ## Not significant Pr(<F) 0.6232
anova(BMI_additive_model,surgeon_full_additive_model) ## Not significant Pr(<F) 0.4046
anova(Diagnosis_additive_model,surgeon_full_additive_model) ## Marginally Significant Pr(<F) 0.067
anova(Side_additive_model,surgeon_full_additive_model) ## *** Significant Pr(<F) 2.2e-16
anova(KneeScore_Patient_additive_model,surgeon_full_additive_model) ## *** Significant Pr(<F) 2.2e-16
anova(GPS_additive_model,surgeon_full_additive_model) ## Not significant Pr(<F) 0.4046
anova(Manufacturer_additive_model,surgeon_full_additive_model) ## Not significant Pr(<F) 0.7153
anova(FemoralComponentModel_additive_model,surgeon_full_additive_model) ## Not significant Pr(<F) 0.1388
anova(FemoralComponentSize_additive_model,surgeon_full_additive_model) ## Not significant Pr(<F) 0.3345
anova(FemoralComponentType_additive_model,surgeon_full_additive_model) ## Not significant Pr(<F) 0.5606
anova(TibialTrayModel_additive_model,surgeon_full_additive_model) ## Not significant Pr(<F) 0.8707
anova(TibialTraySize_additive_model,surgeon_full_additive_model) ## Not significant Pr(<F) 0.1527
anova(TibialInsertModel_additive_model,surgeon_full_additive_model) ## * Some significance Pr(<F) 0.043
anova(TibialInsertWidth_additive_model,surgeon_full_additive_model) ## Not significant Pr(<F) 0.721
anova(TibialInsertType_additive_model,surgeon_full_additive_model) ## Not significant Pr(<F) 0.613
Since there are only a few predictors selected as significant, we can create a smaller model based on those.
surgeon_selective_model = lm(KneeScore_Surgeon ~ Age + Year + Diagnosis + Side + KneeScore_Patient + TibialInsertModel, data = knee)
evaluate(surgeon_selective_model)
## Test Result
## 1 Shapiro Wilk pvalue 1.386481e-07
## 2 Breusch-Pagan pvalue 0.0003592525
## 3 R Squared 0.2236303
## 4 Adj R Squared 0.2091638
## 5 Cross Validation 14.2455
## 6 Large Hat Values 164
anova(surgeon_selective_model, surgeon_full_additive_model)
## Analysis of Variance Table
##
## Model 1: KneeScore_Surgeon ~ Age + Year + Diagnosis + Side + KneeScore_Patient +
## TibialInsertModel
## Model 2: KneeScore_Surgeon ~ Surgeon + Year + Age + Gender + Weight +
## Height + BMI + Diagnosis + Race + Side + KneeScore_Patient +
## GPS + Manufacturer + FemoralComponentModel + FemoralComponentSize +
## FemoralComponentType + TibialTrayModel + TibialTraySize +
## TibialInsertModel + TibialInsertWidth + TibialInsertType +
## PatellaModel + PatellaDiameter
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 1932 386466
## 2 1832 364936 100 21530 1.0808 0.2795
The evaluation doesnt show much improvement and anova rejects the significance of the smaller model.
We will attempt to utilize AIC and BIC approaches and evaluate the results.
n = length(resid(surgeon_full_additive_model))
surgeon_model_back_bic = step(surgeon_full_additive_model, direction = "backward", k = log(n), trace = 0)
surgeon_model_back_aic = step(surgeon_full_additive_model, direction = 'backward', trace =0)
evaluate(surgeon_model_back_bic)
## Test Result
## 1 Shapiro Wilk pvalue 1.169168e-05
## 2 Breusch-Pagan pvalue 0.0001309477
## 3 R Squared 0.1869122
## 4 Adj R Squared 0.1852562
## 5 Cross Validation 14.37628
## 6 Large Hat Values 87
The resulting AIC and BIC models both remove predictors that have influence, but neither approach results in remarkably better Adjusted $\R^2$ or cross validation values.
Next, we evaluate the selected models to see which is significant.
anova(surgeon_model_back_bic, surgeon_full_additive_model)
## Analysis of Variance Table
##
## Model 1: KneeScore_Surgeon ~ Age + Side + KneeScore_Patient
## Model 2: KneeScore_Surgeon ~ Surgeon + Year + Age + Gender + Weight +
## Height + BMI + Diagnosis + Race + Side + KneeScore_Patient +
## GPS + Manufacturer + FemoralComponentModel + FemoralComponentSize +
## FemoralComponentType + TibialTrayModel + TibialTraySize +
## TibialInsertModel + TibialInsertWidth + TibialInsertType +
## PatellaModel + PatellaDiameter
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 1964 404744
## 2 1832 364936 132 39808 1.5139 0.0002399 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
anova(surgeon_model_back_aic, surgeon_full_additive_model)
## Analysis of Variance Table
##
## Model 1: KneeScore_Surgeon ~ Surgeon + Year + Age + Diagnosis + Side +
## KneeScore_Patient
## Model 2: KneeScore_Surgeon ~ Surgeon + Year + Age + Gender + Weight +
## Height + BMI + Diagnosis + Race + Side + KneeScore_Patient +
## GPS + Manufacturer + FemoralComponentModel + FemoralComponentSize +
## FemoralComponentType + TibialTrayModel + TibialTraySize +
## TibialInsertModel + TibialInsertWidth + TibialInsertType +
## PatellaModel + PatellaDiameter
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 1947 392113
## 2 1832 364936 115 27177 1.1863 0.09206 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
anova(surgeon_model_back_bic, surgeon_model_back_aic)
## Analysis of Variance Table
##
## Model 1: KneeScore_Surgeon ~ Age + Side + KneeScore_Patient
## Model 2: KneeScore_Surgeon ~ Surgeon + Year + Age + Diagnosis + Side +
## KneeScore_Patient
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 1964 404744
## 2 1947 392113 17 12631 3.6892 4.963e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
The BIC model is significant versus the full model, but the AIC model is not. Similarly, the BIC model is significant versus the AIC model.
showPlots(surgeon_model_back_bic)
Next we will explore some interactions.
surgeon_knee_int = lm(KneeScore_Surgeon ~ Surgeon + KneeScore_Patient:Age:Diagnosis * Side:TibialInsertModel, data = knee)
evaluate(surgeon_knee_int)
## Test Result
## 1 Shapiro Wilk pvalue 2.350093e-10
## 2 Breusch-Pagan pvalue 1
## 3 R Squared 0.2841152
## 4 Adj R Squared 0.2267501
## 5 Cross Validation 16.19673
## 6 Large Hat Values 189
showPlots(surgeon_knee_int)
surgeon_knee_int2 = lm(KneeScore_Surgeon ~ Surgeon + Age:Race + Side * KneeScore_Patient * TibialInsertModel, data = knee)
evaluate(surgeon_knee_int2)
## Test Result
## 1 Shapiro Wilk pvalue 1.827287e-08
## 2 Breusch-Pagan pvalue 0.2186727
## 3 R Squared 0.266134
## 4 Adj R Squared 0.2235224
## 5 Cross Validation 15.23952
## 6 Large Hat Values 198
Finally, we remove the large Cook’s values to see if any models can further improve.
data_no_cooks = removeCooks(surgeon_knee_int, knee)
surgeon_knee_int_no_influence = lm(KneeScore_Surgeon ~ Surgeon + KneeScore_Patient:Age:Diagnosis * Side:TibialInsertModel, data = data_no_cooks)
evaluate(surgeon_knee_int_no_influence)
## Test Result
## 1 Shapiro Wilk pvalue 1.019117e-10
## 2 Breusch-Pagan pvalue 1
## 3 R Squared 0.2939026
## 4 Adj R Squared 0.239711
## 5 Cross Validation 13.33974
## 6 Large Hat Values 159
surgeon_knee_int_2_no_influence = lm(KneeScore_Surgeon ~ Surgeon + Age:Race + Side * KneeScore_Patient * TibialInsertModel, data = data_no_cooks)
evaluate(surgeon_knee_int_2_no_influence)
## Test Result
## 1 Shapiro Wilk pvalue 1.152983e-08
## 2 Breusch-Pagan pvalue 0.7075876
## 3 R Squared 0.2727118
## 4 Adj R Squared 0.2313148
## 5 Cross Validation 13.59516
## 6 Large Hat Values 165
Removing the large Cook’s values did help improve the model. The first interaction model has a better evaluation than the second, but let’s compare them.
anova(surgeon_knee_int_no_influence, surgeon_knee_int_2_no_influence)
## Analysis of Variance Table
##
## Model 1: KneeScore_Surgeon ~ Surgeon + KneeScore_Patient:Age:Diagnosis *
## Side:TibialInsertModel
## Model 2: KneeScore_Surgeon ~ Surgeon + Age:Race + Side * KneeScore_Patient *
## TibialInsertModel
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 1759 317819
## 2 1792 327357 -33 -9538.1 1.5997 0.017 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Depending on how strict we select our \(\alpha\), this test may be significant.
Finally, we will evaluate the RMSE of the best performing models thus far.
options(warn=-1)
set.seed(5021)
sample_idx = sample(1:nrow(knee), round(nrow(knee)*.50))
train_data = knee[sample_idx, ]
test_data = knee[-sample_idx, ]
full_test = rmse(test_data$KneeScore_Surgeon, predict(surgeon_full_additive_model, test_data))
full_train = rmse(train_data$KneeScore_Surgeon, predict(surgeon_full_additive_model, train_data))
sel_test = rmse(test_data$KneeScore_Surgeon, predict(surgeon_selective_model, test_data))
sel_train = rmse(train_data$KneeScore_Surgeon, predict(surgeon_selective_model, train_data))
bic_test = rmse(test_data$KneeScore_Surgeon, predict(surgeon_model_back_bic, test_data))
bic_train = rmse(train_data$KneeScore_Surgeon, predict(surgeon_model_back_bic, train_data))
int_test = rmse(test_data$KneeScore_Surgeon, predict(surgeon_knee_int, test_data))
int_train = rmse(train_data$KneeScore_Surgeon, predict(surgeon_knee_int, train_data))
int2_test = rmse(test_data$KneeScore_Surgeon, predict(surgeon_knee_int2, test_data))
int2_train = rmse(train_data$KneeScore_Surgeon, predict(surgeon_knee_int2, train_data))
# Need to resample the data since the smaller models were created with Cooks values removed
sample_idx_2 = sample(1:nrow(data_no_cooks), round(nrow(data_no_cooks)*.50))
train_data_2 = data_no_cooks[sample_idx_2, ]
test_data_2 = data_no_cooks[-sample_idx_2, ]
no_inf_test = rmse(test_data_2$KneeScore_Surgeon, predict(surgeon_knee_int_no_influence, test_data))
no_inf_train = rmse(train_data_2$KneeScore_Surgeon, predict(surgeon_knee_int_no_influence, train_data))
| Model | Train RMSE | Test RMSE |
|---|---|---|
Model All+ |
13.2827766 | 13.9370302 |
Selective Model |
13.6632464 | 14.3477076 |
Model BIC |
14.008548 | 14.6583575 |
Interction Model |
13.1714794 | 13.7285362 |
Interaction Two |
13.354649 | 13.8818568 |
Interaction No Influence |
17.7542193 | 18.559902 |
Begin by fitting a full additive model as a baseline and evaluating.
pat_full_additive_model = lm(KneeScore_Patient ~ ., data = knee)
evaluate(pat_full_additive_model)
## Test Result
## 1 Shapiro Wilk pvalue 3.998615e-14
## 2 Breusch-Pagan pvalue 0.9114347
## 3 R Squared 0.3134365
## 4 Adj R Squared 0.2624689
## 5 Cross Validation 17.09153
## 6 Large Hat Values 178
The baseline model has many large leverage values, which, upon inspection may be coming from several of the implant fields. Perhaps removing the suspect implant predictors will help improve the model.
pat_model_eight = lm(KneeScore_Patient ~ . - Diagnosis - Race - TibialInsertType - FemoralComponentModel - PatellaModel - TibialTrayModel - TibialTraySize - FemoralComponentType, data = knee)
evaluate(pat_model_eight)
## Test Result
## 1 Shapiro Wilk pvalue 1.691281e-14
## 2 Breusch-Pagan pvalue 0.0009795814
## 3 R Squared 0.278013
## 4 Adj R Squared 0.2541363
## 5 Cross Validation 16.90839
## 6 Large Hat Values 134
anova(pat_model_eight, pat_full_additive_model)
## Analysis of Variance Table
##
## Model 1: KneeScore_Patient ~ (Surgeon + Year + Age + Gender + Weight +
## Height + BMI + Diagnosis + Race + Side + KneeScore_Surgeon +
## GPS + Manufacturer + FemoralComponentModel + FemoralComponentSize +
## FemoralComponentType + TibialTrayModel + TibialTraySize +
## TibialInsertModel + TibialInsertWidth + TibialInsertType +
## PatellaModel + PatellaDiameter) - Diagnosis - Race - TibialInsertType -
## FemoralComponentModel - PatellaModel - TibialTrayModel -
## TibialTraySize - FemoralComponentType
## Model 2: KneeScore_Patient ~ Surgeon + Year + Age + Gender + Weight +
## Height + BMI + Diagnosis + Race + Side + KneeScore_Surgeon +
## GPS + Manufacturer + FemoralComponentModel + FemoralComponentSize +
## FemoralComponentType + TibialTrayModel + TibialTraySize +
## TibialInsertModel + TibialInsertWidth + TibialInsertType +
## PatellaModel + PatellaDiameter
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 1905 533186
## 2 1832 507026 73 26160 1.2948 0.05006 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
While removing the predictors doesn’t yield a definitively better model at a reasonable \(\alpha\), it does increase adjusted \(R^2\) and slightly improves the cross validation score.
Utilize a Bayesian Information Criterion approach and evaluate the selected model.
n = length(resid(pat_full_additive_model))
pat_model_back_bic = step(pat_full_additive_model, direction = "backward", k = log(n), trace = 0)
evaluate(pat_model_back_bic)
## Test Result
## 1 Shapiro Wilk pvalue 1.934109e-15
## 2 Breusch-Pagan pvalue 7.743929e-07
## 3 R Squared 0.2423439
## 4 Adj R Squared 0.2404141
## 5 Cross Validation 16.91176
## 6 Large Hat Values 112
anova(pat_model_back_bic, pat_full_additive_model)
## Analysis of Variance Table
##
## Model 1: KneeScore_Patient ~ Age + Gender + Weight + Height + KneeScore_Surgeon
## Model 2: KneeScore_Patient ~ Surgeon + Year + Age + Gender + Weight +
## Height + BMI + Diagnosis + Race + Side + KneeScore_Surgeon +
## GPS + Manufacturer + FemoralComponentModel + FemoralComponentSize +
## FemoralComponentType + TibialTrayModel + TibialTraySize +
## TibialInsertModel + TibialInsertWidth + TibialInsertType +
## PatellaModel + PatellaDiameter
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 1963 559528
## 2 1832 507026 131 52502 1.4481 0.000998 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
The resulting BIC model scores marginally better in cross validation though the Anova results clearly prefer the full additive model.
Next, we will evaluate the RMSE of the best performing models thus far.
options(warn=-1)
sample_idx = sample(1:nrow(knee), 1500)
train_data = knee[sample_idx, ]
test_data = knee[-sample_idx, ]
rmse = function(actual, predicted) { sqrt(mean((actual - predicted) ^ 2))}
pat_full_test = rmse(test_data$KneeScore_Patient, predict(pat_full_additive_model, test_data))
pat_full_train = rmse(train_data$KneeScore_Patient, predict(pat_full_additive_model, train_data))
pat_test_mod_8 = rmse(test_data$KneeScore_Patient, predict(pat_model_eight, test_data))
pat_train_mod_8 = rmse(train_data$KneeScore_Patient, predict(pat_model_eight, train_data))
pat_bic_test = rmse(test_data$KneeScore_Patient, predict(pat_model_back_bic, test_data))
pat_bic_train = rmse(train_data$KneeScore_Patient, predict(pat_model_back_bic, train_data))
| Model | Train RMSE | Test RMSE |
|---|---|---|
Model All+ |
13.2827766 | 13.9370302 |
Model 8 |
16.7304474 | 15.5444358 |
Model BIC |
17.1636879 | 15.8376175 |
The additive model with all predictors has the best RMSE values.
Perhaps removing some influential data points will assist in creating a better model. Next we remove influential points that satisfy the criteria for Large Leverage, Large Cook's Distance and Large RStandard values.
new_dataframe = removeCooks(pat_model_eight, knee)
pat_model_nine = lm(KneeScore_Patient ~ . - Diagnosis - Race - TibialInsertType - FemoralComponentModel - PatellaModel - TibialTrayModel - TibialTraySize - FemoralComponentType, data = new_dataframe)
evaluate(pat_model_nine)
## Test Result
## 1 Shapiro Wilk pvalue 1.850696e-11
## 2 Breusch-Pagan pvalue 0.003703422
## 3 R Squared 0.3046738
## 4 Adj R Squared 0.2805915
## 5 Cross Validation 15.46164
## 6 Large Hat Values 99
This does seem to be of moderate benefit in relation to adjusted \(R^2\) and cross validation.
We will evaluate the same model once again after removing the identified large hat values.
remove_hat_dataframe = removeLargeHatValues(pat_model_nine, new_dataframe)
pat_model_ten = lm(KneeScore_Patient ~ . - Diagnosis - Race - TibialInsertType - FemoralComponentModel - PatellaModel - TibialTrayModel - TibialTraySize - FemoralComponentType, data = remove_hat_dataframe)
evaluate(pat_model_ten)
## Test Result
## 1 Shapiro Wilk pvalue 4.598428e-10
## 2 Breusch-Pagan pvalue 0.0008076549
## 3 R Squared 0.2994217
## 4 Adj R Squared 0.2837551
## 5 Cross Validation 15.74152
## 6 Large Hat Values 101
Removing the large hat values decreased the cross validation score. Perhaps we can find a better model by running BIC on the modified dataframe.
n = length(resid(pat_model_ten))
pat_model_10_bic = step(pat_model_ten, direction = "backward", k = log(n), trace = 0)
evaluate(pat_model_10_bic)
## Test Result
## 1 Shapiro Wilk pvalue 9.247251e-11
## 2 Breusch-Pagan pvalue 0.0004430615
## 3 R Squared 0.2731032
## 4 Adj R Squared 0.271059
## 5 Cross Validation 15.76344
## 6 Large Hat Values 92
Despite the reduction in leveraged values the selected model is no better than preceding models.
sample_idx_two = sample(1:nrow(remove_hat_dataframe), 1300)
train_data_two = remove_hat_dataframe[sample_idx_two, ]
test_data_two = remove_hat_dataframe[-sample_idx_two, ]
test_mod_10 = rmse(test_data_two$KneeScore_Patient, predict(pat_model_ten, train_data_two))
train_mod_10 = rmse(train_data_two$KneeScore_Patient, predict(pat_model_ten, train_data_two))
test_10_bic = rmse(test_data_two$KneeScore_Patient, predict(pat_model_10_bic, train_data_two))
train_10_bic = rmse(train_data_two$KneeScore_Patient, predict(pat_model_10_bic, train_data_two))
| Model | Train RMSE | Test RMSE |
|---|---|---|
Model 10 |
15.5016425 | 20.9075928 |
Model 10 BIC |
15.7155855 | 20.6828703 |
Interestingly, again the models fit on the dataset without leverages fares worse on test RMSE.
# Warning: takes hours to fit
all_two_way_model = lm(KneeScore_Surgeon ~ .*., data = knee)
# Warning...
n = length(resid(all_two_way_model))
model_one_bic_all_two_way = step(all_two_way_model, direction = 'backward', k = log(n), trace = 0)
model_one_pat = lm(log(KneeScore_Patient) ~ ., data = knee)
model_one_surg = lm(log(KneeScore_Surgoen) ~ ., data = knee)
model_two_exp_pat= lm(KneeScore_Patient^2 ~ ., data = knee)
model_two_exp_surg = lm(KneeScore_Surgeon^2 ~ ., data = knee)
# replace all 0 values with the mean of the vector
temp = knee
temp$KneeScore_Patient[temp$KneeScore_Patient == 0] = mean(temp$KneeScore_Patient)
model_three = lm(KneeScore_Patient ~ ., data = temp)
evaluate(model_five)
clean_temp_data = removeLargeHatValues(model_three, temp)
model_four = lm(KneeScore_Patient ~ ., data = clean_temp_data)
evaluate(model_four)
pat_model = lm(KneeScore_Patient ~. , data = knee)
pat_model_back_aic = step(pat_model, direction = "backward", trace = 0)
pat_model_for_aic = step(pat_model, direction = "forward", trace = 0)
pat_model_both_aic = step(pat_model, direction = "both", trace = 0)
n = length(resid(pat_model))
pat_model_back_bic = step(pat_model, direction = "backward", k = log(n), trace = 0)
pat_model_for_bic = step(pat_model, direction = "forward", k = log(n), trace = 0)
pat_model_both_bic = step(pat_model, direction = "both", k = log(n), trace = 0)
evaluate(pat_model)
evaluate(pat_model_back_aic)
evaluate(pat_model_for_aic)
evaluate(pat_model_both_aic)
## Adj.R.Squared: 0.24 - CrossValidation: 16.9
evaluate(pat_model_back_bic)
evaluate(pat_model_for_bic)
evaluate(pat_model_both_bic)
## Remove large leverages
new_data = removeInfluential(pat_model, knee)
pat_model_without_large_leverages = lm(KneeScore_Patient ~ ., data = new_data)
evaluate(pat_model_without_large_leverages)
Removing the records with large hat values and refitting simply introduces more large hat values. What is unique about these rows of data?
which(hatvalues(pat_model) == 1.0)
print(knee[17,])
print(knee[240,])
print(knee[638,])
print(knee[1093,])
print(knee[1775, ])
pat_model_nine = lm(KneeScore_Patient ~ . - TibialInsertType - FemoralComponentModel - PatellaModel - TibialTrayModel - FemoralComponentType, data = knee)
evaluate(pat_model_nine)